﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcMasterPage.Entity;
using Commons.Entity.CommonEnums;
using Commons.Entity;
using System.Linq.Expressions;

namespace MvcMasterPage.BLL
{
    public class UserManager : BLLBase
    {
        /// <summary>
        /// 业务层得到完整的用户信息
        /// </summary>
        /// <returns></returns>
        public List<User> GetUser()
        {
            var linq = from data1 in LoadRepository.GetEntities<User>().Where(i => i.Status != (int)Status.Deleted).OrderByDescending(i => i.ID)
                       join data2 in LoadRepository.GetEntities<User_Profile>() on data1.ID equals data2.ID
                       join data3 in LoadRepository.GetEntities<Review>(i => i.ObjType == 0) on data1.ID equals data3.ObjID into list
                       select new User_Ext
                       {
                           ID = data1.ID,
                           Name = data1.Name,
                           Password = data1.Password,
                           Review = list,
                           Status = data1.Status,
                           User_Profile = data2,
                       };
            return linq.ToList<User>();
        }

        public PagedList<User> GetUser(Expression<Func<User, bool>> predicate, PagingParam pp)
        {
            var linq = LoadRepository.GetEntities<User>();
            if (predicate != null)
                linq = linq.Where(predicate);
            linq = from data1 in linq.Where(i => i.Status != (int)Status.Deleted).OrderByDescending(i => i.ID)
                   join data3 in LoadRepository.GetEntities<Review>(i => i.ObjType == 0) on data1.ID equals data3.ObjID into list
                   select new User_Ext
                   {
                       ID = data1.ID,
                       Name = data1.Name,
                       Password = data1.Password,
                       Review = list,
                       Status = data1.Status,
                   };
            return new PagedList<User>(linq, pp.PageIndex, pp.PageSize);
        }


    }
}